package io.aicn.lowcode.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.aicn.lowcode.dto.ProductCreate;
import io.aicn.lowcode.dto.ProductUpdate;
import io.aicn.lowcode.entity.Product;
import io.aicn.lowcode.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> implements IProductService {
    
    @Autowired
    private ProductMapper productMapper;
    
    public List<Product> searchProducts(String name, String code, String category, BigDecimal minPrice, BigDecimal maxPrice, Integer minStock, Integer maxStock, String status) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(name)) {
            queryWrapper.like(Product::getName, name);
        }
        if (StringUtils.hasText(code)) {
            queryWrapper.like(Product::getCode, code);
        }
        if (StringUtils.hasText(category)) {
            queryWrapper.like(Product::getCategory, category);
        }
        if (minPrice != null) {
            queryWrapper.ge(Product::getPrice, minPrice);
        }
        if (maxPrice != null) {
            queryWrapper.le(Product::getPrice, maxPrice);
        }
        if (minStock != null) {
            queryWrapper.ge(Product::getStock, minStock);
        }
        if (maxStock != null) {
            queryWrapper.le(Product::getStock, maxStock);
        }
        if (StringUtils.hasText(status)) {
            queryWrapper.eq(Product::getStatus, status);
        }
        
        return productMapper.selectList(queryWrapper);
    }
    
    public Product createProduct(ProductCreate productCreate) {
        Product product = new Product();
        product.setName(productCreate.getName());
        product.setCode(productCreate.getCode());
        product.setDescription(productCreate.getDescription());
        product.setPrice(productCreate.getPrice());
        product.setCategory(productCreate.getCategory());
        product.setStock(productCreate.getStock());
        product.setStatus(productCreate.getStatus());
        product.setCreated_at(LocalDateTime.now());
        product.setUpdated_at(LocalDateTime.now());
        
        productMapper.insert(product);
        return product;
    }
    
    public Product updateProduct(Long id, ProductUpdate productUpdate) {
        Product product = productMapper.selectById(id);
        if (product == null) {
            return null;
        }
        
        if (productUpdate.getName() != null) {
            product.setName(productUpdate.getName());
        }
        if (productUpdate.getCode() != null) {
            product.setCode(productUpdate.getCode());
        }
        if (productUpdate.getDescription() != null) {
            product.setDescription(productUpdate.getDescription());
        }
        if (productUpdate.getPrice() != null) {
            product.setPrice(productUpdate.getPrice());
        }
        if (productUpdate.getCategory() != null) {
            product.setCategory(productUpdate.getCategory());
        }
        if (productUpdate.getStock() != null) {
            product.setStock(productUpdate.getStock());
        }
        if (productUpdate.getStatus() != null) {
            product.setStatus(productUpdate.getStatus());
        }
        
        product.setUpdated_at(LocalDateTime.now());
        productMapper.updateById(product);
        return product;
    }
    
    public boolean deleteProduct(Long id) {
        return productMapper.deleteById(id) > 0;
    }
    
    public List<Product> getAllProducts() {
        return productMapper.selectList(null);
    }
    
    public Product getById(Long id) {
        return productMapper.selectById(id);
    }
}